package com.leo.dao;

import java.util.List;

import javax.annotation.Resource;

import com.leo.entity.Admin;
import com.leo.entity.User;

public class AdminDaoImp implements AdminDao {
	@Resource
	private Dao dao;
	
	@Override
	public long getCount(boolean checkQueryRight, String searchKey, String keyType){
		keyType = keyType == null ? "username" : keyType;
		searchKey = searchKey == null ? "" : searchKey;
		searchKey = "%" + searchKey + "%";
		if(checkQueryRight)
			return dao.getDataCount(User.class, "queryRight=? and "+ keyType +" like ?", "no",searchKey);
		else 
			return   dao.getDataCount(User.class, keyType +" like ?", searchKey);
	}
	
	@Override
	public Admin findAdmin(String username){
		return dao.find(Admin.class, "username = ?", username);
	}
	
	@Override
	public void addAdmin(Admin admin){
		dao.save(admin);
	}
	
	@Override
	public List listUsers(boolean checkQueryRight, String searchKey, String keyType,int pageIndex, int pageSize){
		
		List<User> list = null;
		keyType = keyType == null ? "username" : keyType;
		searchKey = searchKey == null ? "" : searchKey;
		searchKey = "%" + searchKey + "%";
		if(checkQueryRight)
			list = dao.getScrollData(User.class, "queryRight=? and " + keyType + " like ? order by username asc", pageIndex, pageSize, "no",searchKey);
		else 
			list = dao.getScrollData(User.class, keyType + " like ? order by username asc", pageIndex, pageSize, searchKey);
		return list;
	}
	
	@Override
	public User findUser(String username){
		return dao.find(User.class, "username=?", username);
	}

	@Override
	public void updateUser(User u){
		dao.update(u);
	}
	
	@Override
	public void addUser(User u){
		dao.save(u);
	}
	@Override
	public void deleteUsers(String usernameSet){
		dao.delete(User.class, "username", usernameSet.split(","));
	}
}
